{% extends "base_jrcadmin2.html" %}

{% block mainheading %}{{ block.super }} \ Module Permissions{% endblock %}

{% block content %}

    <table>
        <tr>
            <th>Username</th>
            <th>Is staff</th>
            <th>Is active</th>
            <th>E-mail</th>
            <th>Markets</th>
            <th>Trading-systems</th>
            <th>PET</th>
            <th>Import</th>
            <th></th>
        </tr>
    {% for u in object_list %}
        <tr class="{% cycle "row1" "row2" %}">
            <td>{{ u.username }}</td>
            <td>{{ u.is_staff }}</td>
            <td>{{ u.is_active }}</td>
            <td>{{ u.email }}</td>
            <td id="td_{{ u.pk }}_{{ markets_perm_pk }}">
                {# u.has_markets_perm #}
                <input type="checkbox" {% if u.has_markets_perm %} checked="checked" {% endif %} onchange="OnJrcPermissionChanged({{ u.pk }}, {{ markets_perm_pk }});" />
            </td>
            <td id="td_{{ u.pk }}_{{ tradingsystems_perm_pk }}">
                <input type="checkbox" {% if u.has_tradingsystems_perm %} checked="checked" {% endif %} onchange="OnJrcPermissionChanged({{ u.pk }}, {{ tradingsystems_perm_pk }});" />
            </td>
            <td id="td_{{ u.pk }}_{{ pet_perm_pk }}">
                <input type="checkbox" {% if u.has_pet_perm %} checked="checked" {% endif %} onchange="OnJrcPermissionChanged({{ u.pk }}, {{ pet_perm_pk }});" />
            </td>
            <td id="td_{{ u.pk }}_{{ import_perm_pk }}">
                <input type="checkbox" {% if u.has_import_perm %} checked="checked" {% endif %} onchange="OnJrcPermissionChanged({{ u.pk }}, {{ import_perm_pk }});" />
            </td>
            <td></td>
        </tr>
    {% empty %}
        No users!
    {% endfor %}
    </table>


<script type="text/javascript">

    function OnJrcPermissionChanged(userId, permId)
    {
        //alert(jQuery("#up_" + userId + "_" + permId).attr("checked"));
        jQuery.ajax({
            url: "/user/dochangeperm/" + userId + "/" + permId
            , cache: false
            , success: function(data){ OnJrcPermissionCompleted(userId, permId, data); }
            , error: function(){ alert("Sorry, error has occured!"); }
        });
        //alert("Permission " + permId + " is changed for user: " + userId);
    }
    
    function OnJrcPermissionCompleted(userId, permId, data)
    {
        jQuery("#td_" + userId + "_" + permId).html(data);
    }

</script>

{% endblock %}
